package view;

import controller.TrainController;
import entity.BrainTrain;
import entity.Phase;

import javax.swing.*;
import java.awt.*;

/**
 * @author Ayase
 * @date 2020/8/9-20:59
 */
public class MyFrame extends JFrame {
    private static MyFrame instance = null;

    public static MyFrame getInstance(){
        if(instance == null){
            instance = new MyFrame();
        }
        return instance;
    }

    private TrainController trainController;

    private static JTextPane text = new JTextPane();

    private static String htmlDoc;

    private Dimension screenSize;

    private int inset;

    private MyFrame(){
        //获取屏幕尺寸
        screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        inset = 200;
        trainController = new TrainController(screenSize.width-inset*3);

        CardLayout card = new CardLayout();
        JPanel mainPanel = new JPanel(card);

        //构建初始界面
        JPanel welcomePage = new JPanel();
        JButton btStart = new JButton("START");
        btStart.addActionListener(e->{
            setHtmlDoc(trainController.getPhaseHtml());
            card.show(mainPanel,"train");
        });
        welcomePage.add(btStart);

        //构造回答界面
        JPanel answerP = new JPanel();

        answerP.setSize(screenSize.width-inset*2,screenSize.height-inset*2);
        answerP.setLayout(new GridLayout(1,1));
        Box box = Box.createVerticalBox();
        JTextField[] answerArea = new JTextField[10];
        JLabel[] pNo = new JLabel[10];
        for(int i = 0 ; i < answerArea.length ; i++){
            Box boxH = Box.createHorizontalBox();
            box.add(Box.createVerticalStrut(30));
            pNo[i] = new JLabel((i+1)+":");
            pNo[i].setVisible(false);
            answerArea[i] = new JTextField();
            answerArea[i].setVisible(false);

            boxH.add(Box.createHorizontalStrut(10));
            boxH.add(pNo[i]);
            boxH.add(Box.createHorizontalStrut(10));
            boxH.add(answerArea[i]);
            boxH.add(Box.createHorizontalStrut(10));

            box.add(boxH);
            box.add(Box.createVerticalStrut(5));
        }

        JLabel lb = new JLabel("正确率：");
        JTextField tfCr = new JTextField();

        tfCr.setSize(30,200);
        tfCr.setEditable(false);

        Box boxH1 = Box.createHorizontalBox();
        boxH1.add(Box.createHorizontalStrut(10));
        boxH1.add(lb);
        boxH1.add(Box.createHorizontalStrut(10));
        boxH1.add(tfCr);
        boxH1.add(Box.createHorizontalStrut(10));
        box.add(boxH1);
        box.add(Box.createVerticalStrut(5));

        JTextField tfAn = new JTextField();
        tfAn.setEditable(false);

        Box boxH2 = Box.createHorizontalBox();
        boxH2.add(Box.createHorizontalStrut(10));
        boxH2.add(new JLabel("答案："));
        boxH2.add(Box.createHorizontalStrut(10));
        boxH2.add(tfAn);
        boxH2.add(Box.createHorizontalStrut(10));
        box.add(boxH2);

        box.add(Box.createVerticalStrut(50));

        JButton btNextT = new JButton("Next");
        btNextT.setVisible(false);
        btNextT.addActionListener(e->{
            for(int i = 0 ; i <trainController.getDifficult();i++){
                answerArea[i].setText("");
                answerArea[i].setVisible(false);
                pNo[i].setVisible(false);
            }

            Double cr = Double.parseDouble(tfCr.getText());
            trainController.nextTrain(cr);
            setHtmlDoc(trainController.getPhaseHtml());

            tfAn.setText("");
            tfCr.setText("");

            validate();
            card.show(mainPanel,"train");
            btNextT.setVisible(false);
        });

        JButton btDone = new JButton("Done");
        btDone.addActionListener(e->{
            String[] answers = new String[answerArea.length];
            for(int i = 0 ; i < answerArea.length ; i++){
                answers[i] = answerArea[i].getText();
            }
            tfCr.setText(String.valueOf(trainController.calcuCR(answers)));
            tfAn.setText(trainController.getAnswers());
            btNextT.setVisible(true);
            validate();
        });



        box.add(btDone);
        box.add(Box.createVerticalStrut(5));
        box.add(btNextT);
        box.add(Box.createVerticalStrut(100));

        answerP.add(box);

        //构建阅读界面
        JPanel textPane = new JPanel();
        textPane.setBounds(720,400,0,0);

        text.setContentType("text/html");
        text.setEditable(false);

        JButton btNext = new JButton("Next");
        btNext.addActionListener(e->{
            String h = trainController.getPhaseHtml();
            if(h!=null) {
                setHtmlDoc(h);
            }
            else{
                for(int i = 0 ; i < trainController.getDifficult() ; i++){
                    answerArea[i].setVisible(true);
                    pNo[i].setVisible(true);
                }
                validate();
                card.show(mainPanel,"answer");
            }
        });

        textPane.add(text);
        textPane.add(btNext);



        //添加卡片
        mainPanel.add(welcomePage,"welcome");
        mainPanel.add(textPane,"train");
        mainPanel.add(answerP,"answer");

        //以下添加各容器及窗口设定
        add(mainPanel);


        //设置主窗口的大小
        setBounds(inset,inset,screenSize.width-inset*2,screenSize.height-inset*2);

        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        validate();
        setVisible(true);
    }

    /**
     * @MethodName setHtmlDoc
     * @Description TODO 设置html并插入到text中
     * @Param [html]
     * @Return void
     * @author Ayase
     * @date 14:16
     */
    public void setHtmlDoc(String html){
        htmlDoc = html;
        text.setText(htmlDoc);
        validate();
    }
}
